package com.yanqu.road.server.cmd.cross.player;

import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import io.netty.channel.ChannelHandlerContext;

@Cmd(code = Protocol.S_CROSS_TO_SEARCH_PLAYER, desc = "跨服查看玩家")
public class CrossSearchPlayerCmd implements Command {

	@Override
	public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
		PlayerProto.CrossSearchPlayerReqMsg reqMsg = PlayerProto.CrossSearchPlayerReqMsg.parseFrom(packet.getBytes());
		long userId = reqMsg.getUserId();
		long serverId = reqMsg.getServerId();
		long searchUserAliasId = reqMsg.getSearchUserAliasId();
		UserInfo userInfo = UserMgr.getUserInfoByAliasId(searchUserAliasId);
		PlayerProto.PlayerSimpleTempMsg.Builder userMsg = PlayerPb.parsePlayerSimpleTempMsg(userInfo);
		PlayerProto.CrossSearchPlayerRespMsg.Builder respMsg = PlayerProto.CrossSearchPlayerRespMsg.newBuilder();
		respMsg.setUserId(userId);
		respMsg.setServerId(serverId);
		if (null != userMsg) {
			respMsg.setRet(0);
			respMsg.setPlayerData(userMsg);
		} else {
			respMsg.setRet(GameErrorCode.E_PLAYER_NO_FOUND);
		}
		YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_SEARCH_PLAYER_RESP, respMsg);
		GamePlayerMgr.sendPacket(0, pbMsg);
	}
}
